// Package sys
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2025 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
// @AutoGenerate Version 2.15.11
package sys

import (
	"context"
	"fmt"
	"hotgo/internal/dao"
	"hotgo/internal/library/hgorm"
	"hotgo/internal/library/hgorm/handler"
	"hotgo/internal/model/input/form"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"
	"hotgo/utility/convert"
	"hotgo/utility/excel"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/util/gconv"
)

type sSysWalletWithdrawalLog struct{}

func NewSysWalletWithdrawalLog() *sSysWalletWithdrawalLog {
	return &sSysWalletWithdrawalLog{}
}

func init() {
	service.RegisterSysWalletWithdrawalLog(NewSysWalletWithdrawalLog())
}

// Model 钱包-提现记录ORM模型
func (s *sSysWalletWithdrawalLog) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
	return handler.Model(dao.WalletWithdrawalLog.Ctx(ctx), option...)
}

// List 获取钱包-提现记录列表
func (s *sSysWalletWithdrawalLog) List(ctx context.Context, in *sysin.WalletWithdrawalLogListInp) (list []*sysin.WalletWithdrawalLogListModel, totalCount int, err error) {
	mod := s.Model(ctx)

	// 字段过滤
	mod = mod.FieldsPrefix(dao.WalletWithdrawalLog.Table(), sysin.WalletWithdrawalLogListModel{})
	mod = mod.Fields(hgorm.JoinFields(ctx, sysin.WalletWithdrawalLogListModel{}, &dao.Store, "store"))
	mod = mod.Fields(hgorm.JoinFields(ctx, sysin.WalletWithdrawalLogListModel{}, &dao.AdminMember, "adminMember"))

	// 关联表字段
	mod = mod.LeftJoinOnFields(dao.Store.Table(), dao.WalletWithdrawalLog.Columns().StoreId, "=", dao.Store.Columns().Id)
	mod = mod.LeftJoinOnFields(dao.AdminMember.Table(), dao.WalletWithdrawalLog.Columns().MemberId, "=", dao.AdminMember.Columns().Id)

	// 查询id
	if in.Id > 0 {
		mod = mod.Where(dao.WalletWithdrawalLog.Columns().Id, in.Id)
	}

	// 查询申请时间
	if len(in.CreatedAt) == 2 {
		mod = mod.WhereBetween(dao.WalletWithdrawalLog.Columns().CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
	}

	// 查询门店名称
	if in.StoreName != "" {
		mod = mod.WherePrefixLike(dao.Store.Table(), dao.Store.Columns().Name, in.StoreName)
	}

	// 查询门店电话
	if in.StorePhone != "" {
		mod = mod.WherePrefixLike(dao.Store.Table(), dao.Store.Columns().Phone, in.StorePhone)
	}

	// 查询微信昵称
	if in.AdminMemberNickname != "" {
		mod = mod.WherePrefixLike(dao.AdminMember.Table(), dao.AdminMember.Columns().Nickname, in.AdminMemberNickname)
	}

	//

	// 分页
	mod = mod.Page(in.Page, in.PerPage)

	// 排序
	mod = mod.OrderDesc(dao.WalletWithdrawalLog.Table() + "." + dao.WalletWithdrawalLog.Columns().Id)

	// 查询数据
	if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
		err = gerror.Wrap(err, "获取钱包-提现记录列表失败，请稍后重试！")
		return
	}
	return
}

// Export 导出钱包-提现记录
func (s *sSysWalletWithdrawalLog) Export(ctx context.Context, in *sysin.WalletWithdrawalLogListInp) (err error) {
	list, totalCount, err := s.List(ctx, in)
	if err != nil {
		return
	}

	// 字段的排序是依据tags的字段顺序，如果你不想使用默认的排序方式，可以直接定义 tags = []string{"字段名称", "字段名称2", ...}
	tags, err := convert.GetEntityDescTags(sysin.WalletWithdrawalLogExportModel{})
	if err != nil {
		return
	}

	var (
		fileName  = "导出钱包-提现记录-" + gctx.CtxId(ctx)
		sheetName = fmt.Sprintf("索引条件共%v行,共%v页,当前导出是第%v页,本页共%v行", totalCount, form.CalPageCount(totalCount, in.PerPage), in.Page, len(list))
		exports   []sysin.WalletWithdrawalLogExportModel
	)

	if err = gconv.Scan(list, &exports); err != nil {
		return
	}

	err = excel.ExportByStructs(ctx, tags, exports, fileName, sheetName)
	return
}

// Delete 删除钱包-提现记录
func (s *sSysWalletWithdrawalLog) Delete(ctx context.Context, in *sysin.WalletWithdrawalLogDeleteInp) (err error) {

	if _, err = s.Model(ctx).WherePri(in.Id).Unscoped().Delete(); err != nil {
		err = gerror.Wrap(err, "删除钱包-提现记录失败，请稍后重试！")
		return
	}
	return
}

// View 获取钱包-提现记录指定信息
func (s *sSysWalletWithdrawalLog) View(ctx context.Context, in *sysin.WalletWithdrawalLogViewInp) (res *sysin.WalletWithdrawalLogViewModel, err error) {
	if err = s.Model(ctx).WherePri(in.Id).Scan(&res); err != nil {
		err = gerror.Wrap(err, "获取钱包-提现记录信息，请稍后重试！")
		return
	}
	return
}